#include <stdlib.h>

#include <debug.h>
#include <file_api.h>

static void file_api_test(void *param)
{
    FILE_ID fd;
    unsigned file_count;
    size_t sz;

    fd = FDI_fopen("filecount.txt", "rb");
    if (fd)
    {
        LOG_PRINTF("%s: open() read file OK, fd %d", __func__, fd);
        sz = FDI_fread(&file_count, 1, sizeof(file_count), fd);
        if (sz != sizeof(file_count))
        {
            LOG_PRINTF("%s: read() file error", __func__);
            file_count = 0;
        }
        FDI_fclose(fd);
    }
    else
    {
        LOG_PRINTF("%s: open() read file error", __func__);
        file_count = 0;
    }
    LOG_PRINTF("%s: read() file_count=%u", __func__, file_count);

    file_count++;

    fd = FDI_fopen("filecount.txt", "wb");
    if (fd)
    {
        LOG_PRINTF("%s: open() write file OK, fd %d", __func__, fd);
        sz = FDI_fwrite(&file_count, 1, sizeof(file_count), fd);
        if (sz != sizeof(file_count))
        {
            LOG_PRINTF("%s: write() file error", __func__);
        }
        FDI_fclose(fd);
    }
    else
    {
        LOG_PRINTF("%s: open() write file error", __func__);
    }
    LOG_PRINTF("%s: write() file_count=%u", __func__, file_count);
}

int main()
{
    file_api_test(NULL);

    return 0;
}
